package com.zero.yygh.user.api;

import com.zero.yygh.common.result.Result;
import com.zero.yygh.common.util.AuthContextHolder;
import com.zero.yygh.common.utils.IpUtil;
import com.zero.yygh.model.user.UserInfo;
import com.zero.yygh.user.service.UserInfoService;
import com.zero.yygh.vo.user.LoginVo;
import com.zero.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;
import java.util.Map;

/**
 * @author zero
 * @desc 用户信息Controller
 * @create 2021/4/2
 */
@Api("用户信息接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result<Map<String, Object>> login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    @ApiOperation("用户认证接口")
    @PostMapping("auth/userAuth")
    public Result<Object> userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    @ApiOperation("获取用户信息接口")
    @GetMapping("auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

}
